<template>
  <div class="banner">
    <div class="banner-content" v-if="banners.length > 0">
      <div class="banner-slides" :style="{ transform: 'translateX(-' + (currentIndex * 100) + '%)' }">
        <div v-for="banner in banners" :key="banner.id" class="banner-slide">
          <a :href="banner.link" target="_blank">
            <img :src="'http://127.0.0.1:8000' + banner.image" :alt="banner.title" class="banner-image">
            <div class="banner-text-content">
              <h1 class="banner-title">{{ banner.title }}</h1>
              <p class="banner-note">{{ banner.note }}</p>
              <button class="try-btn">立即查看</button>
            </div>
          </a>
        </div>
      </div>
    </div>
    <div class="banner-nav">
      <span 
        v-for="(banner, index) in banners" 
        :key="banner.id"
        class="nav-dot"
        :class="{ active: currentIndex === index }"
        @click="setCurrentSlide(index)"
      ></span>
    </div>
    <div class="banner-arrows">
      <button class="arrow-left" @click="prevSlide">&lt;</button>
      <button class="arrow-right" @click="nextSlide">&gt;</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import axios from 'axios'

const banners = ref([])
const currentIndex = ref(0)
let autoplayInterval = null

const fetchBanners = async () => {
  try {
    const response = await axios.get('http://127.0.0.1:8000/api/banners/')
    if (response.data.code === 1) {
      banners.value = response.data.data.sort((a, b) => b.weight - a.weight)
    } else {
      console.error('获取轮播图数据失败:', response.data.message)
    }
  } catch (error) {
    console.error('请求轮播图数据出错:', error)
  }
}

const nextSlide = () => {
  currentIndex.value = (currentIndex.value + 1) % banners.value.length
}

const prevSlide = () => {
  currentIndex.value = currentIndex.value === 0 ? banners.value.length - 1 : currentIndex.value - 1
}

const setCurrentSlide = (index) => {
  currentIndex.value = index
}

const startAutoplay = () => {
  autoplayInterval = setInterval(() => {
    nextSlide()
  }, 5000)
}

onMounted(() => {
  fetchBanners()
  startAutoplay()
})

onBeforeUnmount(() => {
  if (autoplayInterval) {
    clearInterval(autoplayInterval)
  }
})
</script>

<style scoped>
.banner {
  width: 100%;
  height: 500px;
  position: relative;
  overflow: hidden;
}

.banner-content {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}

.banner-slides {
  display: flex;
  width: 100%;
  height: 100%;
  position: relative;
  transition: transform 0.5s ease;
}

.banner-slide {
  min-width: 100%;
  height: 100%;
  flex-shrink: 0;
  position: relative;
}

.banner-slide a {
  text-decoration: none;
  color: white;
  display: block;
  height: 100%;
  width: 100%;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-text-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  z-index: 2;
  width: 80%;
  max-width: 800px;
}

.banner-title {
  font-size: 36px;
  margin-bottom: 15px;
  text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}

.banner-note {
  font-size: 18px;
  margin-bottom: 20px;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}

.try-btn {
  background: #1890ff;
  color: white;
  border: none;
  padding: 12px 30px;
  font-size: 16px;
  border-radius: 25px;
  cursor: pointer;
  transition: background 0.3s;
}

.try-btn:hover {
  background: #40a9ff;
}

.banner-nav {
  position: absolute;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 3;
}

.nav-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255,255,255,0.5);
  cursor: pointer;
  transition: all 0.3s;
}

.nav-dot.active {
  background: white;
  transform: scale(1.2);
}

.banner-arrows {
  position: absolute;
  width: 100%;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  justify-content: space-between;
  padding: 0 30px;
  z-index: 3;
}

.arrow-left, .arrow-right {
  background: rgba(0,0,0,0.3);
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 20px;
  transition: background 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.arrow-left:hover, .arrow-right:hover {
  background: rgba(0,0,0,0.5);
}

/* 添加渐变遮罩，使文字更清晰 */
.banner-slide::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(rgba(0,0,0,0.3), rgba(0,0,0,0.3));
  z-index: 1;
}
</style> 